package com.cytangaa.youlike.database;

import java.sql.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DbAdapter 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_VIDEO_ID = "video_id";
    public static final String KEY_UPLOADED_DATE = "uploaded_date";   
    public static final String KEY_CATEGORY = "category";   
    public static final String KEY_TITLE = "title";    
    public static final String KEY_THUNBNAIL = "thumbnail";   
    public static final String KEY_DURATION = "duration";
    public static final String KEY_SHAREDLINK= "share_link";
    public static final String KEY_RTSP = "rtsp";    
    public static final String KEY_VIEWCOUNT = "view_count";   

    public static final String KEY_ADDED_DATE = "added_date";   
    
    
    private static final String TAG = "DBAdapter";
    
    private static final String DATABASE_NAME = "video_database";
    private static final String DATABASE_TABLE = "favourite_video";
//    private static final String DATABASE_FAVO_TABLE = "video";

    private static final int DATABASE_VERSION = 1;

    
    private static final String DATABASE_CREATE_FAVO =
            "create table favourite_video (_id integer primary key autoincrement, "
            + "video_id text not null, uploaded_date text not null, category text not null, title text not null, " 
            + "thumbnail text not null, duration text not null, share_link text not null, rtsp text not null, "
            + "view_count text not null, added_date text not null);";
        
    private final Context context; 
    
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DbAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
        
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            Log.w(TAG, "In onCreate");
            //db.execSQL(DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE_FAVO);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }    
    
    //---opens the database---
    public DbAdapter open() throws SQLException 
    {
        Log.w(TAG, "In open()");

        db = DBHelper.getWritableDatabase();
        Log.w(TAG, "In open() 2"+db);

        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }
    
    //---insert a title into the database---
    public long insertVideo(String uploaded_date, String videoId, String category, String title, String thumbnail,
    		String duration, String share_link, String rtsp, String view_count, String added_date) 
    {
        Log.w(TAG, "In insert");

        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_UPLOADED_DATE, uploaded_date);        
        initialValues.put(KEY_VIDEO_ID, videoId);                
        initialValues.put(KEY_CATEGORY, category);
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_THUNBNAIL, thumbnail);
        initialValues.put(KEY_DURATION, duration);
        initialValues.put(KEY_SHAREDLINK, share_link);
        initialValues.put(KEY_RTSP, rtsp);
        initialValues.put(KEY_VIEWCOUNT, view_count);
        initialValues.put(KEY_ADDED_DATE, added_date);
        
        Log.w(TAG, "Right before insert, Check: "+added_date+" "+category+" "
        +title+" "+thumbnail+" " +duration+" ");
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular title---
    public boolean deleteByDateTitle(Date date) 
    {
        return db.delete(DATABASE_TABLE, KEY_ADDED_DATE + 
        		"<" + date, null) > 0;
    }
    public boolean deleteTitle(String video_id) 
    {
        return db.delete(DATABASE_TABLE, KEY_VIDEO_ID + 
        		"='" + video_id+"'", null) > 0;
    }
    
    //---deletes a all title---
    public boolean deleteAllTitle() 
    {
        return db.delete(DATABASE_TABLE, null, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllVideo() 
    {
        return db.query(DATABASE_TABLE, new String[] {
        		KEY_ROWID, 
        		KEY_VIDEO_ID,
        		KEY_UPLOADED_DATE,
        		KEY_CATEGORY,
        		KEY_TITLE,
        		KEY_THUNBNAIL,
        		KEY_DURATION,
        		KEY_SHAREDLINK, 
        		KEY_RTSP,
        		KEY_VIEWCOUNT,   
        		KEY_ADDED_DATE
                },
                null, 
                null, 
                null, 
                null, 
                KEY_UPLOADED_DATE+" DESC");
    }
    public Cursor getTenVideo(String offset) 
    {
        return db.query(DATABASE_TABLE, new String[] {
        		KEY_ROWID, 
        		KEY_VIDEO_ID,
        		KEY_UPLOADED_DATE,
        		KEY_CATEGORY,
        		KEY_TITLE,
        		KEY_THUNBNAIL,
        		KEY_DURATION,
        		KEY_SHAREDLINK, 
        		KEY_RTSP,
        		KEY_VIEWCOUNT,
        		KEY_ADDED_DATE
                }, null, null, null, null,  KEY_ADDED_DATE+" DESC", offset+", 11");
    }

    //---retrieves a particular title---
    public Cursor getTitle(String videoId) throws SQLException 
    {
    	Log.i("in DbAdpter","in detTitle driverId: "+videoId+"  "+KEY_TITLE);
    	Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                		KEY_ROWID, 
                		KEY_VIDEO_ID,
                		KEY_UPLOADED_DATE,
                		KEY_CATEGORY,
                		KEY_TITLE,
                		KEY_THUNBNAIL,
                		KEY_DURATION,
                		KEY_SHAREDLINK, 
                		KEY_RTSP,
                		KEY_VIEWCOUNT,
                		KEY_ADDED_DATE
                		}, 
                		KEY_VIDEO_ID + "='" + videoId+"'", 
                		null,
                		null, 
                		null, 
                		null, 
                		null);
    	

        
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    public boolean updateTitle(String videoId, String uploaded_date, String category, String title, String thumbnail,
    		String duration, String share_link, String rtsp, String view_count, String add_date) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_VIDEO_ID, videoId);        
        args.put(KEY_UPLOADED_DATE, uploaded_date);
        args.put(KEY_CATEGORY, category);
        args.put(KEY_TITLE, title);
        args.put(KEY_THUNBNAIL, thumbnail);
        args.put(KEY_DURATION, duration);
        args.put(KEY_SHAREDLINK, share_link);
        args.put(KEY_RTSP, rtsp);
        args.put(KEY_VIEWCOUNT, view_count);
        args.put(KEY_ADDED_DATE, add_date);


        return db.update(DATABASE_TABLE, args, 
        		KEY_VIDEO_ID + "='" + KEY_VIDEO_ID+"'", null) > 0;
    }
   

    
}